/**
 * Created by huanggw on 2015/11/1.
 */
var  client = require('../database');
var  uid = require('../utils/uuid');//用于生成id

function Post(name) {
    this.name = name;
}

mysql = client.getDbCon();
module.exports = Post;

Post.get =  function(sql, callback) {
    mysql.query(sql, function(err, result) {
        if(err){
            console.log('[SELECT ERROR] - ',err.message);
            return;
        }
        console.log('---------------SELECT----------------');
        //console.log(result);
        callback(result);
    })
}

Post.update = function(target, condition, callback) {
    if(!target) target = "*";
    if(!condition) var sql = "select "+ target+ " from schools;"
    else var sql = "select "+ target+ " from schools where "+condition;
    mysql.query(sql, function(err, result) {
        if(err){return;}
        var currentnum = result[0].likenum+1;
        var updatesql = 'update schools set likenum='+currentnum+' where '+ condition;
        mysql.query(updatesql, function(err, result) {
            if(err){
                console.log('[UPDATE ERROR] - ',err.message);
                return;
            }
            console.log('---------------LIKENUM UPDATED----------------');
            console.log("likenum: " +currentnum);
            callback([{likenum: currentnum}])
        })
    });
}

Post.addcomment = function(params, callback) {
    var sql = "INSERT INTO comments (cityname, schoolname,campusname,gatename,comment, user, time) VALUES (?,?,?,?,?,?,?);"
    //var param = [params[0], params[1], params[2], params[3]];
    mysql.query(sql, params, function (err, result) {
        if(err){
            console.log('[INSERT ERROR] - ',err.message);
            return;
        }
        console.log('-------ADD COMMENT----------');
        callback();
    })
}

Post.addLike = function(params) {
    var sql = "INSERT INTO likes (cityname, schoolname, campusname, gatename, user, time) VALUES (?,?,?,?,?,?);"
    mysql.query(sql, params, function (err, result) {
        if(err){
            console.log('[INSERT ERROR] - ',err.message);
            return;
        }
        console.log('-------ADD LIKE----------');
    })
}